﻿<%@ Page Title="" Language="C#" MasterPageFile="~/administrator/backend.Master" AutoEventWireup="true"
    CodeBehind="product-add.aspx.cs" Inherits="VQWEB.administrator.products.product_add" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Content_backend" runat="server">
    <form id="frmObjects" action="" method="post">
    <div id="col8" class="col-md-8"></div>
    <div id="col4" class="col-md-4">
    </div>
    </form>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Script_backend" runat="server">
   <script type="text/javascript">
       ShowToolBar("edit");
       var url_ashx = "/admin/product/handler";
       var url_input = '<%=ResolveUrl("~/administrator/templates/input.tpl") %>';

       $(document).ready(function () {
           $.get(url_ashx, { view_product: "view_product", Id: GetId() }, function (data) {
               var parse = $.parseJSON(data);
               var tag = parse.tag;
               var category = parse.category;
               var array_category,array_tag;
               if (CheckAtionInPath("add")) {
                   array_category = array_checkbox(category);
                   array_tag = array_checkbox(tag);
                   loadform(0, "", "", "", "", 0, 0, 0, new Date(), false, array_category, array_tag);
               }
               else if (CheckAtionInPath("edit")) {
                   var product = parse.product;
                   var tag = parse.tag;
                   array_category = array_checkbox(category);
                   array_tag = array_checkbox(tag);
                   loadform(product.id, product.title, product.description, product.content, product.image, product.price, product.discounts, product.hit, product.date, product.active, array_category,array_tag);

               }

           });
       });
       function loadform(id, title, description, content, image,price,discounts, hit, date, active, array_category,array_tag) {
           $.jqoteload(url_input, function (tmpl) {
               $("#col4").multiple("groupcheckbox", null, array_category, "Category:", tmpl.input_tpl);
               $("#col4").multiple("groupcheckbox", null, array_tag, "tag:", tmpl.input_tpl);
               $("#col8").single("hidden", "product_id", id, null, null, tmpl.input_tpl);
               $("#col8").single("text", "product_title", title, "Tên sản phẩm:", null, tmpl.input_tpl);
               $("#col8").single("textarea", "product_description", description, "Mo tả:", null, tmpl.input_tpl);
               $("#col8").single("editor", "product_content", content, "Chi tiết sản phẩm:", null, tmpl.input_tpl);
               $("#col8").single("text", "product_img", image, "Hình ảnh:", null, tmpl.input_tpl);
               $("#col8").single("text", "product_price", price, "Giá sản phẩm:", null, tmpl.input_tpl);
               $("#col8").single("text", "product_discounts", discounts, "Giá khuyến mại:", null, tmpl.input_tpl);
               $("#col8").single("text", "product_hit", hit, "Lượt xem:", null, tmpl.input_tpl);
               $("#col8").single("text", "product_date", date, "Ngày đăng:", null, tmpl.input_tpl);
               $("#col8").single("checkbox", "product_active", true, "Kich hoat", active, tmpl.input_tpl);
               tinymce.init({ selector: 'textarea#tinymce' });
               $("#frmObjects").validate({
                   rules: { product_title: { required: true} },
                   messages: { product_title: { required: null} }
               });
               $("input[name='product_date']").mask("99/99/9999");
           });

       }


       $(document).on("click", "#btnCancel", function () {
           window.location = "/admin/product/list";
       });
       $(document).on("click", "#btnSave", function () {
           if ($("#frmObjects").valid()) {
               var value = tinymce.get('tinymce').getContent();
               $("textarea[name='product_content']").val(value);
               var arr = $("#frmObjects").serializeObject();
               $.post(url_ashx, { add_product: window.escape(arr) }, function (data) {
                   alert(data);
                   window.location = "/admin/product/list";
               });
           }
       });
    </script>
</asp:Content>
